﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DataTransfer;
namespace Gui
{
    public partial class frmDanhSachKhamBenh : DevComponents.DotNetBar.Office2007Form
    {
        frmMain frm_main;
        public frmDanhSachKhamBenh(frmMain frm)
        {
            frm_main = frm;
            InitializeComponent();
           
        }
        private DSKBBus dskbBus;
        private void frmDanhSachKhamBenh_Load(object sender, EventArgs e)
        {
            loadAll();
        }


        public void load()
        {
            dskbBus=new DSKBBus();
            DateTime key = dtpNgayKham.Value.Date;
            dtgDanhSachBenhNhan.DataSource = dskbBus.GetByDay(key);
        }
        public void loadAll()
        {
            dskbBus = new DSKBBus();
            
            dtgDanhSachBenhNhan.DataSource = dskbBus.GetAll();
        
        }
        private void dtpNgayKham_ValueChanged(object sender, EventArgs e)
        {
            load();

        }

        private void btnThemBenhNhan_Click(object sender, EventArgs e)
        {
            Form frm = new frmThemBenhNhanKhamBenh(this);
            frm.ShowDialog();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSuaBenhNhan_Click(object sender, EventArgs e)
        {
           
            
            
            DSKB info = new DSKB();
            if (getItem(ref info) == true)
            {
                Form frm = new frmThemBenhNhanKhamBenh(this,info);
                frm.ShowDialog();
            }
        }
        private bool getItem(ref DSKB info)
        {
            bool flag = false;
            int index = dtgDanhSachBenhNhan.CurrentRow.Index;
            if (index >= 0)
            {

                info.ID = Convert.ToInt32(dtgDanhSachBenhNhan["ID", index].Value.ToString());
                info.HoTen = dtgDanhSachBenhNhan["HoTen", index].Value.ToString();
                info.GioiTinh = Convert.ToInt32(dtgDanhSachBenhNhan["GioiTinh", index].Value.ToString());
                info.NamSinh = Convert.ToDateTime(dtgDanhSachBenhNhan["NamSinh", index].Value.ToString());
                info.DiaChi = dtgDanhSachBenhNhan["DiaChi", index].Value.ToString();
                info.NgayKham = Convert.ToDateTime(dtgDanhSachBenhNhan["NgayKham", index].Value.ToString());
                info.DaKham = Convert.ToInt32(dtgDanhSachBenhNhan["DaKham", index].Value.ToString());
                flag = true;
            }
            if (flag == false)
            {
                MessageBox.Show("Chua chon Item");
            }
            return flag;
        }

        private void btnXoaBenhNhan_Click(object sender, EventArgs e)
        {
            DSKB info = new DSKB() ;
            if (getItem(ref info) == true)
            {
                         try 
	                    {	        
		                    if(dskbBus.Delele(info.ID))
                            {
                                MessageBox.Show("Delete Success");
                                load();
                            }

	                    }
	                    catch (Exception ex)
	                    {
                            MessageBox.Show("Delete Error:" + ex.Message);
		                    //throw;
	                    } 
            }
            
        }

        private void btnLapPhieuKham_Click(object sender, EventArgs e)
        {
            
            DSKB info = new DSKB();
            if (getItem(ref info) == true)
            {
                frm_main.LapPhieuKhamBenh(info);
            }
           
        }

       
    }
}
